package com.hltx.service.user.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.dubbo.config.annotation.Service;
import com.hltx.mapper.user.CompanyUserMapper;
import com.hltx.mapper.user.UserMapper;
import com.hltx.pojo.user.SupplierReal;
import com.hltx.pojo.user.User;
import com.hltx.pojo.user.UserAccount;
import com.hltx.pojo.user.UserBank;
import com.hltx.pojo.user.UserLogin;
import com.hltx.pojo.user.UserRealName;
import com.hltx.service.user_dubbo.CompanyUserManager;
import com.hltx.util.CustomUtil;
import com.hltx.util.DateUtil;
import com.hltx.util.ResultInfo;
import com.hltx.util.ResultUtil;


/**
 * CompanyUserService
 * @version 2017.06.08 17:40
 * @author 史蓓蕾
 */
@Service (protocol = "dubbo", loadbalance = "roundrobin")
public class CompanyUserService implements CompanyUserManager {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private CompanyUserMapper companyUserMapper;
	
	@Override
	public Map<String, Object> saveUserRegister(UserLogin login) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			int num = userMapper.findByPhone(login);
			if (num > 0) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "此用户已存在!"));
				return resMap;
			}
			userMapper.saveUserLogin(login);
			User user = new User();
			user.setCodePre(DateUtil.getYear());
			user.setCodeSuf(CustomUtil.buildRandom(3) + "");
			user.setLoginId(login.getId());
			user.setPhone(login.getUserName());
			String petName = CustomUtil.subPhone(user.getPhone());
			user.setPetName(petName);
			user.setAddress("");
			user.setCreationDate("");
			user.setBirthday("");
			user.setLevel("0");
			user.setIsSupplier("01");
			userMapper.saveUserInfo(user);
			UserAccount account = new UserAccount();
			account.setUserId(user.getId());
			account.setLoginId(login.getId());
			account.setBalance(new BigDecimal(0));
			account.setPoints(0);
			account.setIsPublic("01");
			userMapper.saveUserAccount(account);
			User userN = userMapper.getUser(user.getId());

			resMap.put("user", userN);
			resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "注册成功"));
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "网络异常"));
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}

		return resMap;
	}

	@Override
	public Map<String, Object> getLogin(UserLogin login) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			UserLogin ul = userMapper.getUserLoginByUserName(login.getUserName());
			if (ul == null) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "用户或密码输入有误!"));
				return resMap;
			}
			User userN = userMapper.getUser(ul.getId());
			resMap.put("user", userN);
			resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "登录成功"));
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "网络异常"));
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resMap;
	}

	@Override
	public Map<String, Object> getSupplierReal(int id) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			SupplierReal real = companyUserMapper.getSupplierReal(id);
			if (real == null) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "0"));
			} else if ("01".equals(real.getSteps())) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "1"));
			} else if ("02".equals(real.getSteps())) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "2"));
			} else if ("03".equals(real.getSteps())) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "3"));
			}else if("00".equals(real.getSteps())){
				resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "4"));
				resMap.put("remark", real.getRemark());
			}
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "0"));
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resMap;
	}

	@Override
	public Map<String, Object> saveSupplierReal(SupplierReal realName) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if (realName != null) {
				int id = companyUserMapper.saveSupplierReal(realName);
				if (id <= 0) {
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "保存失败"));
					return resMap;
				}
				resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "保存成功"));
			}
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "网络异常"));
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resMap;
	}

	@Override
	public Map<String, Object> saveUserBank(UserBank bank) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if (bank != null) {
				SupplierReal rea = companyUserMapper.getSupplierReal(bank.getUserId());
				bank.setPhone(rea.getPhone());
				int id = companyUserMapper.saveUserBank(bank);
				if (id <= 0) {
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "保存失败"));
					return resMap;
				}
				SupplierReal real = new SupplierReal();
				real.setSteps("02");
				real.setUpdateTime(DateUtil.getTime());
				real.setUserId(bank.getUserId());
				real.setUpdateBy(rea.getPhone());
				real.setAuditState("11");
				companyUserMapper.updateSupplierSteps(real);
				resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "保存成功"));
			}
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "网络异常"));
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resMap;
	}

	@Override
	public Map<String, Object> saveUserRealName(UserRealName realName) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			if (realName != null) {
				int id = companyUserMapper.saveUserRealName(realName);
				if (id <= 0) {
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "保存失败"));
					return resMap;
				}
				SupplierReal real = new SupplierReal();
				real.setSteps("03");
				real.setUpdateTime(DateUtil.getTime());
				real.setUserId(realName.getUserId());
				real.setUpdateBy(realName.getPhone());
				real.setAuditState("00");
				companyUserMapper.updateSupplierSteps(real);
				resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "保存成功"));
			}
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "网络异常"));
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resMap;
	}

	@Override
	public Map<String, Object> getAuditStateMess(int id) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			SupplierReal real = companyUserMapper.getSupplierReal(id);
			if (real != null) {
				if ("00".equals(real.getAuditState())) {
					resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "2", ""));
				} else if ("01".equals(real.getAuditState())) {
					resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "3", ""));
				} else if ("02".equals(real.getAuditState())) {
					resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "4", ""));
					resMap.put("remark", real.getRemark());
				} else {
					if ("01".equals(real.getSteps())) {
						resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "1", "2"));
					} else if ("02".equals(real.getSteps())) {
						resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "1", "3"));
					}
				}
			} else {
				resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "1", "1"));
			}
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "网络异常"));
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resMap;
	}

	@Override
	public Map<String, Object> getRealNameMessage(int id) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			SupplierReal real = companyUserMapper.getSupplierReal(id);
			UserBank bank = companyUserMapper.getUserBank(id);
			UserRealName name = companyUserMapper.getUserRealName(id);
			if (real != null) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "查询成功"));
				resMap.put("real", real);
			}
			if (bank != null) {
				resMap.put("bank", bank);
			}
			if (name != null) {
				resMap.put("name", name);
			}
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "网络异常"));
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resMap;
	}

	@Override
	public Map<String, Object> updateSupplierReal(SupplierReal realName) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			realName.setUpdateTime(DateUtil.getTime());
			companyUserMapper.updateSupplierReal(realName);
			resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "修改成功"));
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "网络异常"));
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resMap;
	}

	@Override
	public Map<String, Object> updateUserBank(UserBank bank) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			SupplierReal real = companyUserMapper.getSupplierReal(bank.getUserId());
			if (real == null) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "企业未实名"));
				return resMap;
			}
			bank.setPhone(real.getPhone());
			companyUserMapper.updateUserBank(bank);
			resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "修改成功"));
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "网络异常"));
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resMap;
	}

	@Override
	public Map<String, Object> updateUserRealName(UserRealName realName) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			companyUserMapper.updateUserRealName(realName);
			SupplierReal real = new SupplierReal();
			real.setSteps("03");
			real.setUpdateTime(DateUtil.getTime());
			real.setUserId(realName.getUserId());
			real.setUpdateBy(realName.getPhone());
			real.setAuditState("00");
			companyUserMapper.updateSupplierSteps(real);
			resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "修改成功"));
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "网络异常"));
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resMap;
	}

	@Override
	public Map<String, Object> getRealName(SupplierReal realName) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			List<SupplierReal> real = companyUserMapper.getRealNamelistPage(realName);
			if (real.size() > 0) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "查询成功"));
				resMap.put("real", real);
				return resMap;
			}
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "查询失败"));
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "网络异常"));
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resMap;
	}

	@Override
	public Map<String, Object> updateAuditingState(SupplierReal real) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			SupplierReal sr = companyUserMapper.getSupplierReal(real.getUserId());
			if(sr != null){
				real.setVersion(sr.getVersion());
				if("02".equals(real.getAuditState())){
					real.setSteps("00");
				}else{
					real.setSteps("03");
				}
				companyUserMapper.updateAuditingState(real);
				resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "查询成功"));
				return resMap;
			}
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "查询失败"));
		} catch (Exception e) {
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "网络异常"));
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resMap;
	}
}
